package com.liaoban.app.ui.common;

import android.content.Context;
import android.content.Intent;
import android.os.Bundle;
import android.webkit.WebChromeClient;
import android.webkit.WebSettings;
import android.webkit.WebView;
import android.webkit.WebViewClient;

import com.liaoban.app.R;
import com.liaoban.app.constants.ExtraParam;
import com.liaoban.app.helper.ToastHelper;
import com.liaoban.app.ui.base.BaseToolbarActivity;

import java.util.HashMap;
import java.util.Map;

import butterknife.BindView;

/**
 * Created by ybc on 2017/9/13 0013.
 * 加载网页
 */

public class WebActivity extends BaseToolbarActivity {
    @BindView(R.id.wv)
    WebView wv;

    private String mUrl;

    public static Intent newIntent(Context context, String title, String url) {
        Intent intent = new Intent();
        intent.setClass(context, WebActivity.class);
        intent.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK | Intent.FLAG_ACTIVITY_CLEAR_TOP );
        intent.putExtra(ExtraParam.URL, url);
        intent.putExtra(ExtraParam.TITLE, title);
        return intent;
    }

    @Override
    protected String getNewTitle() {
        String title = getIntent().getStringExtra(ExtraParam.TITLE);
        return title;
    }

    @Override
    protected int getLayoutResID() {
        return R.layout.activity_web;
    }

    @Override
    protected void initView(Bundle savedInstanceState) {
        initData();
        initWebView();
        loadUrl(mUrl);
    }

    private void initWebView() {
        WebSettings webSettings = wv.getSettings();
        webSettings.setJavaScriptEnabled(true);
        webSettings.setDomStorageEnabled(true);
        webSettings.setAppCacheMaxSize(1024 * 1024 * 8);
        String appCacheDir = this.getApplicationContext().getDir("cache", Context.MODE_PRIVATE).getPath();
        webSettings.setAppCachePath(appCacheDir);
        webSettings.setAllowFileAccess(true);
        webSettings.setAppCacheEnabled(true);
        webSettings.setCacheMode(WebSettings.LOAD_DEFAULT);
        wv.setWebViewClient(new WebViewClient());
        wv.setWebChromeClient(new WebChromeClient(){
            @Override
            public void onProgressChanged(WebView view, int newProgress) {
                /*if (newProgress == 100) {
                    dismissRunningDialog();
                } else {
                    showRunningDialog();
                }*/
                super.onProgressChanged(view, newProgress);
            }
        });
    }

    private void initData() {
        Intent intent = getIntent();
        if (intent == null) {
            ToastHelper.show(this, R.string.url_no_exist);
            return;
        }
        mUrl = intent.getStringExtra(ExtraParam.URL);
    }

    private void loadUrl(String url) {
        Map<String, String> extraHeaders = new HashMap<>();
        extraHeaders.put("type", "android");
        wv.loadUrl(url, extraHeaders);
    }



    @Override
    public void onBackPressed() {
        if (wv.canGoBack()) {
            wv.goBack();
        } else {
            finish();
        }
    }

}
